<?php
namespace app\controllers;

use \Yii;
use app\controllers\BaseController;
use app\models\Entity;

class EntityController extends BaseController {

	/**
	 * 密码列表
	 *
	 * @return Ambigous <string, string>
	 */
	public function actionList() {
		$query = Entity::find();
		
		$search_title = Yii::$app->request->get('search_title');
		if (!empty($search_title)) {
			$query->andWhere([
				'like',
				'title',
				$search_title
			]);
		}
		
		$pages = new \yii\data\Pagination([
			'totalCount'=>$query->count()
		]);
		$_list = $query->offset($pages->offset)->limit($pages->limit)->all();
		return $this->render('list', [
			'_list'=>$_list,
			'pages'=>$pages
		]);
	}

	/**
	 * 创建新密码
	 *
	 * @return Ambigous <string, string>
	 */
	public function actionCreate() {
		$entity = new Entity();
		if (Yii::$app->request->isPost) {
			if ($entity->load(Yii::$app->request->post()) && $entity->validate()) {
				if ($entity->save()) {
					Yii::$app->session->addFlash('success', '成功保存。');
				} else {
					Yii::$app->session->addFlash('error', '保存失败。');
				}
			} else {
				Yii::$app->session->addFlash('error', '保存失败。');
			}
		}
		return $this->render('edit', [
			'model'=>$entity
		]);
	}
}